package com.suse.ecommerce.controller;

import com.suse.ecommerce.pojo.Order;
import com.suse.ecommerce.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

@RequestMapping("/notify")
@Controller
public class NotifyController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/seckillnotifyAlipay")
    public Object seckillnotifyAlipay(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("接收到支付宝的异步通知请求。。。。。。。。。。");
        String out_trade_no = request.getParameter("out_trade_no");
        String trade_status=request.getParameter("trade_status");
        System.out.println("receive alipay------------------>");
        return null;
    }


    @RequestMapping("/notifyAlipay")
    public void notifyAlipay(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("接收到支付宝的异步通知请求。。。。。。。。。。");
        String out_trade_no = request.getParameter("out_trade_no");
        String trade_status=request.getParameter("trade_status");
        Order order=new Order();
        /*
         * 接受到支付宝的异步通知后，把对应的订单修改
         * */
        Date date = new java.util.Date();
        Timestamp stamp = new Timestamp(date.getTime());
        order.setPaymentTime(stamp);
        order.setStatus(1);
        if(trade_status.equals("TRADE_SUCCESS")) {
            orderService.updateOrder(order, out_trade_no);
        }
        System.out.println(out_trade_no);
        //成功处理后返回success
        try {
            response.getWriter().write("success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
